﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GoShopping.Web.UserControls
{
    public partial class Recenthistory : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DelNullPrdt();
                GetRecentProduct();
                GetRecPrdts();
            }
        }

        private void DelNullPrdt()
        {
            BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
            //IList<Model.Product> pList = pb.GetAllProduct();
            foreach (RecentPrdt prdt in Profile.grpRecent.RecentGoods)
            {
                if (pb.GetProduct(Convert.ToInt32(prdt.RecentId)) == null)
                {
                    Profile.grpRecent.DelRecent(prdt);
                    Profile.Save();
                }
            }
        }

        void GetRecentProduct()
        {
            if (Profile.grpRecent.RecentGoods.Count <= 4)
            {
                lvRecent.DataSource = Profile.grpRecent.RecentGoods;
                lvRecent.DataBind();

                top1.Visible = false;
                down1.Visible = false;
            }
            else
            {
                lvRecent.DataSource = Profile.grpRecent.RecentGoods;
                lvRecent.DataBind();

                top1.Visible = false;
                down1.Visible = true;
            }
            if (Request.Cookies["recent"] != null)
            {
                Response.Cookies["recent"].Value = "rpi=0";
            }
            else
            {
                HttpCookie hc = new HttpCookie("recent");
                hc.Value = "rpi=0";
                Response.Cookies.Add(hc);
            }
        }
        void GetRecPrdts()
        {
            List<ConfigNode> cnList = XMLManipulation.GetConfig(Server.MapPath(prdtConfig), "//Index/Recommand");
            if (cnList.Count <= 4)
            {
                lvPrdts.DataSource = cnList;
                lvPrdts.DataBind();

                right1.Visible = false;
                left.Visible = false;
            }
            else
            {
                lvPrdts.DataSource = cnList.GetRange(0, 4);
                lvPrdts.DataBind();

                right1.Visible = true;
                left.Visible = false;
            }
            if (Request.Cookies["recPrdts"] != null)
            {
                Response.Cookies["recPrdts"].Value = "fpi=0";
            }
            else
            {
                HttpCookie hc = new HttpCookie("recPrdts");
                hc.Value = "fpi=0";
                Response.Cookies.Add(hc);
            }
        }
        private void GetLVRecent(int rpi, List<RecentPrdt> recentList)
        {
            int int1 = recentList.Count / 4;
            if (rpi < int1)
            {
                lvRecent.DataSource = Profile.grpRecent.RecentGoods.GetRange(rpi*4, 4);
                lvRecent.DataBind();
            }
            else
            {
                lvRecent.DataSource = Profile.grpRecent.RecentGoods.GetRange(rpi * 4, recentList.Count % 4);
                lvRecent.DataBind();
            }

        }

        private static string prdtConfig = @"~/Config/PrdtConfig.xml";
        private bool GetLVRec(int fpi,List<ConfigNode> cnList)
        {
            //List<ConfigNode> cnList = XMLManipulation.GetConfig(Server.MapPath(prdtConfig), "//Index/Recommand");
            int int1 = cnList.Count / 4;
            if (fpi < int1)
            {
                //lvPrdts.DataSource = XMLManipulation.GetConfig(Server.MapPath(prdtConfig), "//Index/Recommand").GetRange(fpi*4, 4);
                lvPrdts.DataSource = cnList.GetRange(fpi * 4, 4);
                lvPrdts.DataBind();
                return false;
            }
            else
            {
                lvPrdts.DataSource = cnList.GetRange(fpi * 4, cnList.Count % 4);
                lvPrdts.DataBind();
                return true;
            }
        }
        #region 最近浏览商品向上分页
        protected void top1_Click(object sender, ImageClickEventArgs e)
        {
            string recentCookie = Request.Cookies["recent"].Value;
            string[] str1 = recentCookie.Split('=');
            int rpi = Convert.ToInt16(str1[1]);
            List<RecentPrdt> recentList = Profile.grpRecent.RecentGoods;
            if ((rpi - 1) == 0)
            {
                top1.Visible = false;
                down1.Visible = true;
            }
            else
            {
                top1.Visible = true;
                down1.Visible = true;
            }
            rpi -= 1;
            GetLVRecent(rpi, recentList);
            string cookie = "rpi=" + rpi;
            Response.Cookies["recent"].Value = cookie;
        }
        protected void down1_Click(object sender, ImageClickEventArgs e)
        {
            string recentCookie = Request.Cookies["recent"].Value;
            string[] str1 = recentCookie.Split('=');
            int rpi = Convert.ToInt16(str1[1]);
            List<RecentPrdt> recentList = Profile.grpRecent.RecentGoods;
            if ((rpi + 1) == recentList.Count/4)
            {
                top1.Visible = true;
                down1.Visible = false;
            }
            else
            {
                top1.Visible = true;
                down1.Visible = true;
            }
            rpi += 1;
            GetLVRecent(rpi, recentList);
            string cookie = "rpi=" + rpi;
            Response.Cookies["recent"].Value = cookie;
        }
        #endregion

        #region 推荐商品向左分页
        protected void left_Click(object sender, ImageClickEventArgs e)
        {
            string recentCookie = Request.Cookies["recPrdts"].Value;
            string[] str1 = recentCookie.Split('=');
            int fpi = Convert.ToInt16(str1[1]);
            List<ConfigNode> cnList = XMLManipulation.GetConfig(Server.MapPath(prdtConfig), "//Index/Recommand");

            if ((fpi - 1) == 0)//fpi为第二页,点击后为第一页
            {
                left.Visible = false;
                right1.Visible = true;
            }
            else// if (fpi <= cnList.Count / 4)//fpi为非第二页
            {
                left.Visible = true;
                right1.Visible = true;
            }
            fpi -= 1;
            GetLVRec(fpi, cnList);
            string cookie = "fpi=" + fpi;
            Response.Cookies["recPrdts"].Value = cookie;
        }
        protected void right1_Click(object sender, ImageClickEventArgs e)
        {
            string recentCookie = Request.Cookies["recPrdts"].Value;
            string[] str1 = recentCookie.Split('=');
            //int pz = 4;
            int fpi = Convert.ToInt16(str1[1]);
            List<ConfigNode> cnList = XMLManipulation.GetConfig(Server.MapPath(prdtConfig), "//Index/Recommand");

            if ((fpi + 1) == cnList.Count / 4)//fpi为倒数第二页,点击后为最后一页
            {
                right1.Visible = false;
                left.Visible = true;
            }
            else// if (fpi + 1 > 0)// fpi非倒数第二页
            {
                right1.Visible = true;
                left.Visible = true;
            }
            fpi += 1;
            GetLVRec(fpi, cnList);
            string cookie = "fpi=" + fpi;
            Response.Cookies["recPrdts"].Value = cookie;
        }
        #endregion
    }
}